//
//  StatusTopView.swift
//  weibo
//
//  Created by cmh on 15/9/6.
//  Copyright © 2015年 cmh. All rights reserved.
//

import UIKit
import SDWebImage

class StatusTopView: UIView {

    var statusRowViewModel: StatusRowViewModel? {
        didSet {
            
            // 作者
            nameLabel.text = statusRowViewModel?.status.user?.name
            // 头像
            iconView.sd_setImageWithURL(statusRowViewModel?.userIconUrl)
            // vip
            vipView.image = statusRowViewModel?.userVerifiedImage
            // 会员图标
            memberView.image = statusRowViewModel?.userMemberImage
        
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        
        backgroundColor = UIColor(white: 1.0, alpha: 1.0)
        
        // 1. 添加控件
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(memberView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(vipView)
        
        // 2. 自动布局
        let offset = CGPoint(x: statusCellMargin, y: 0)
        iconView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: self, size: CGSize(width: statusCellIconWidth, height: statusCellIconWidth), offset: CGPoint(x: statusCellMargin, y: statusCellMargin))
        nameLabel.ff_AlignHorizontal(type: ff_AlignType.TopRight, referView: iconView, size: nil, offset: offset)
        memberView.ff_AlignHorizontal(type: ff_AlignType.TopRight, referView: nameLabel, size: nil, offset: offset)
        timeLabel.ff_AlignHorizontal(type: ff_AlignType.BottomRight, referView: iconView, size: nil, offset: offset)
        sourceLabel.ff_AlignHorizontal(type: ff_AlignType.BottomRight, referView: timeLabel, size: nil, offset: offset)
        vipView.ff_AlignInner(type: ff_AlignType.BottomRight, referView: iconView, size: nil, offset: CGPoint(x: 8, y: 8))
    }
    
    // MARK: 懒加载控件
    //  头像，默认占位不图片
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    // 姓名
    private lazy var nameLabel: UILabel = UILabel(title: "姓名", color: UIColor.darkGrayColor(), fontSize: 14)
    // 皇冠
    private lazy var memberView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership_level1"))
    // 发布时间
    private lazy var timeLabel: UILabel = UILabel(title: "刚刚", color: UIColor.orangeColor(), fontSize: 10)
    private lazy var sourceLabel: UILabel = UILabel(title: "来自 新浪微博", color: UIColor.darkGrayColor(), fontSize: 10)
    // vip
    private lazy var vipView: UIImageView = UIImageView(image: UIImage(named: "avatar_grassroot"))

}
